package org.szy.core.common.service.impl;

import org.noear.solon.annotation.ProxyComponent;
import org.noear.solon.core.handle.UploadedFile;
import org.szy.core.common.prop.FileProperties;
import org.szy.core.common.service.FileService;
import org.szy.core.common.uilts.FileUploadUtils;
import org.szy.core.common.uilts.ReqUtil;

import java.io.File;

/**
 * 本地文件上传服务类
 *
 * @author 孙志阳
 * @date 2023-06-13
 * @since 1.0.0
 **/
@ProxyComponent
public class FileServiceImpl implements FileService {


    @Override
    public String uploadFile(UploadedFile file) {
        String dirPath = ReqUtil.getStr("baseDir", "temp");
        if (!dirPath.startsWith(File.separator)) dirPath = File.separator + dirPath;
        return this.uploadFile(file, dirPath);
    }

    @Override
    public String uploadFile(UploadedFile file, String dirPath) {
        if (!dirPath.startsWith(File.separator)) dirPath = File.separator + dirPath;
        String url = FileUploadUtils.upload(FileProperties.localFilePath + dirPath, file);
        if (FileProperties.enableDomain) {
            return FileProperties.domain + File.separator + FileProperties.localFilePrefix + dirPath + url;
        } else {
            return FileProperties.localFilePrefix + dirPath + url;
        }
    }
}
